
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Vector;
import javax.microedition.io.Connector;
import javax.microedition.io.SocketConnection;
import javax.microedition.lcdui.Image;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author Mr Bao
 */
public class Client implements Runnable {

    private SocketConnection socketConnection;
    private OutputStream out;
    private InputStream in;
    private boolean isRunning;
    private UpAnh upAnh;
    public Vector listAlbum;
    public Vector listImageName;
    public Image image;
    public byte[] datas;
    public String register;

    public Client(UpAnh upAnh) {
        try {
            this.upAnh = upAnh;
//            socketConnection = (SocketConnection) Connector.open("socket://58.187.68.68:1234");
            socketConnection = (SocketConnection) Connector.open("socket://localhost:1234");
            in = socketConnection.openInputStream();
            out = socketConnection.openOutputStream();
            isRunning = true;
            new Thread(this).start();
        } catch (Exception e) {
            upAnh.DisplayException(e);
            e.printStackTrace();
        }
    }

    private void Close() {
        try {
            socketConnection.close();
            in.close();
            out.close();
            isRunning = false;
        } catch (Exception e) {
            upAnh.DisplayException(e);
            e.printStackTrace();
        }
    }

    public void run() {
        while (isRunning) {
            try {
                int lenght = in.available();
                if (lenght > 0) {
                    byte[] data = new byte[lenght];
                    int count = in.read(data);
                    while (count < lenght) {
                        count += in.read(data, count, lenght - count);
                    }
                    String message = new String(data);
                    System.out.println(message);
                    Vector messages = Split(message, ":");
                    if (message.startsWith(MessageType.Login)) {
                        upAnh.UserId = Integer.parseInt(messages.elementAt(1).toString());
                    } else if (message.startsWith(MessageType.ListAlbum)) {
                        for (int i = 1; i < messages.size() - 1; i++) {
                            listAlbum.addElement(messages.elementAt(i));
                        }
                        listAlbum.addElement("ENDALBUM");
                    } else if (message.startsWith(MessageType.ListImageName)) {
                        for (int i = 1; i < messages.size() - 1; i++) {
                            listImageName.addElement(messages.elementAt(i));
                        }
                        listImageName.addElement("ENDIMAGENAME");
                    } else if (message.startsWith(MessageType.ViewImage)) {
                        int n = Integer.parseInt(messages.elementAt(1).toString());
                        datas = new byte[n];
                        int c = in.read(datas);
                        while (c < n) {
                            c += in.read(datas, c, n - c);
                        }
//                        data = images;
                        image = Image.createImage(datas, 0, datas.length);
                    } else if (message.startsWith(MessageType.Register)) {
                        register = message.substring(message.indexOf(":") + 1);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                Close();
            }
            System.gc();
        }
    }

    public void Login(String username, String password) {
        try {
            String message = MessageType.Login + ":" + username + ":" + password;
            out.write(message.getBytes());
        } catch (Exception e) {
            upAnh.DisplayException(e);
            Close();
        }
    }

    public static Vector Split(String str, String str1) {
        Vector vector = new Vector();
        while (true) {
            if (str.indexOf(str1) == -1) {
                vector.addElement(str);
                break;
            }
            vector.addElement(str.substring(0, str.indexOf(str1)));
            str = str.substring(str.indexOf(str1) + 1);
        }
        return vector;
    }

    public void Register(String username, String password) {
        try {
            String message = MessageType.Register + ":" + username + ":" + password;
            out.write(message.getBytes());
        } catch (Exception e) {
            upAnh.DisplayException(e);
            Close();
        }
    }

    public void ViewAlbum() {
        try {
            listAlbum = new Vector();
            out.write(MessageType.ListAlbum.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            Close();
        }
    }

    public void ViewImageName(int indexAlbum) {
        try {
            listImageName = new Vector();
            out.write((MessageType.ListImageName + ":" + indexAlbum).getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            Close();
        }
    }

    public void ViewImageNext() {
        try {
            out.write(MessageType.ViewImageNext.getBytes());
        } catch (Exception e) {
        }
    }

    public void ViewImagePre() {
        try {
            out.write(MessageType.ViewImagePre.getBytes());
        } catch (Exception e) {
        }
    }

    public void ViewImage(int indexImage) {
        try {
            out.write((MessageType.ViewImage + ":" + indexImage).getBytes());
        } catch (Exception e) {
        }
    }

    public void ViewNewUpload() {
        try {
            listImageName = new Vector();
            out.write(MessageType.NewUpload.getBytes());
        } catch (Exception e) {
        }
    }

    public void CreateAlbum(String albumName) {
        try {
            out.write((MessageType.CreateAlbum + ":" + albumName).getBytes());
        } catch (Exception e) {
            upAnh.DisplayException(e);
            Close();
        }
    }

    public void SendImage(String nameImage,byte[] images) {
        try {
            out.write((MessageType.UploadImage + ":" + images.length + ":png:" + upAnh.albumIndex +":" + nameImage).getBytes());
            out.write(images);
        } catch (Exception e) {
            upAnh.DisplayException(e);
            Close();
        }
    }

    public void DeleteAlbum(int indexAlbum) {
        try {
            out.write((MessageType.DeleteAlbum + ":" + indexAlbum).getBytes());
        } catch (Exception e) {
        }
    }

    public void DeleteImage(int indexImage) {
        try {
            out.write((MessageType.DeleteImage + ":" + indexImage).getBytes());
        } catch (Exception e) {
        }
    }

    public void Exit(){
        try {
            out.write(MessageType.Exit.getBytes());
        } catch (Exception e) {
        }
    }

    public void Timkiem(String string) {
        try {
            listImageName = new Vector();
            out.write((MessageType.Timkiem +":" + string).getBytes());
        } catch (Exception e) {
        }
    }
}
